package com.csii.mobilebanking.expandablelistview.fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.csii.mobilebanking.R;
import com.csii.mobilebanking.expandablelistview.adapter.FinancialListViewAdapter;
import com.csii.mobilebanking.expandablelistview.adapter.SignedListViewAdapter;
import com.csii.mobilebanking.expandablelistview.entity.Product;
import com.csii.mobilebanking.expandablelistview.entity.Signed;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Financial product management 客户信息管理
 * 
 * @author hua
 * 
 */
@SuppressLint("Recycle")
public class FinancialPMFragment extends BaseFragment implements
		OnClickListener {
	private static final String MAIN_ACTIVITY = "FinancialPMFragment";

	private Spinner sp_fpm_type, sp_signedn_type, sp_signedn_status;
	private Button btn_fip_quuery, btn_signed_query, btn_signed_affirm;
	private ListView lv_financial_data, lv_signed_data;
	// 选择时间
	private Button btn_signedn_start, btn_signedn_end, btn_signed_create,btn_signed_ok,btn_signed_back;
	private EditText et_signedn_start, et_signedn_end;

	@SuppressWarnings("static-access")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.d(MAIN_ACTIVITY, "onCreateView_CustomerIMtFragment--"
				+ this.getArguments().getInt("index"));
		// 初始化系列
		initView(inflater, R.array.title_fpm, R.array.title_fpm_tag,
				R.array.title_fpm_ids, R.array.title_fpm_layout,
				R.array.tab_fpm_view_ids);
		if (container == null)
			return null;

		// 产品类型
		sp_fpm_type = (Spinner) tabHost.findViewById(R.id.sp_fpm_type);

		ArrayAdapter<String> simpleAdapter = new ArrayAdapter<String>(
				getActivity(), R.layout.spinner_item, R.id.tv_idcard_item,
				getResources().getStringArray(R.array.sp_financial_type));
		sp_fpm_type.setAdapter(simpleAdapter);

		// 查询
		btn_fip_quuery = (Button) tabHost.findViewById(R.id.btn_fip_query);
		btn_fip_quuery.setOnClickListener(this);

		tabHost.inflate(getActivity(), R.layout.financial_search_list,
				tabHost.getTabContentView());
		tabHost.inflate(getActivity(), R.layout.financial_detailed_information,
				tabHost.getTabContentView());
		tabHost.inflate(getActivity(), R.layout.financial_signed_list,
				tabHost.getTabContentView());
		tabHost.inflate(getActivity(), R.layout.financial_signed_create,
				tabHost.getTabContentView());
		tabHost.inflate(getActivity(), R.layout.financial_signed_info,
				tabHost.getTabContentView());
		tabHost.inflate(getActivity(), R.layout.financial_signed_succeed,
				tabHost.getTabContentView());
		
		tabHost.addTab(tabHost.newTabSpec("3")
				.setContent(R.id.tab_fpm_view_search_list).setIndicator("3"));
		tabHost.addTab(tabHost.newTabSpec("4")
				.setContent(R.id.tab_fpm_view_detailed_information)
				.setIndicator("4"));
		tabHost.addTab(tabHost.newTabSpec("5")
				.setContent(R.id.tab_fpm_view_signedn_list).setIndicator("5"));
		tabHost.addTab(tabHost.newTabSpec("6")
				.setContent(R.id.tab_fpm_view_signed_create).setIndicator("6"));

		tabHost.addTab(tabHost.newTabSpec("7")
				.setContent(R.id.tab_fpm_view_signed_info).setIndicator("7"));
		tabHost.addTab(tabHost.newTabSpec("8")
				.setContent(R.id.tab_fpm_view_signed_succceed).setIndicator("8"));

		lv_financial_data = (ListView) tabHost
				.findViewById(R.id.lv_financial_data);

		View view = inflater.inflate(R.layout.financail_search_list_item, null);

		// 设置标题 产品查询
		String[] titles = getResources().getStringArray(
				R.array.financial_list_query_title);
		TextView tv_fip_name = (TextView) view.findViewById(R.id.tv_fip_name);
		tv_fip_name.setText(titles[0]);
		TextView tv_fip_deadline = (TextView) view
				.findViewById(R.id.tv_fip_deadline);
		tv_fip_deadline.setText(titles[1]);
		TextView tv_fip_income = (TextView) view
				.findViewById(R.id.tv_fip_income);
		tv_fip_income.setText(titles[2]);
		TextView tv_fip_currency = (TextView) view
				.findViewById(R.id.tv_fip_currency);
		tv_fip_currency.setText(titles[3]);
		TextView tv_fip_data = (TextView) view.findViewById(R.id.tv_fip_date);
		tv_fip_data.setText(titles[4]);

		TextView tv_fip_remark = (TextView) view
				.findViewById(R.id.tv_fip_remark);
		tv_fip_remark.setText(titles[5]);
		lv_financial_data.addHeaderView(view, null, false);
		List<Product> data = new ArrayList<Product>();

		for (int i = 0; i < 6; i++) {
			data.add(new Product("2013开发式理财", "7天", "5.00%", "人民币", new Date(),
					"交易期"));
		}
		FinancialListViewAdapter adapter = new FinancialListViewAdapter(
				getActivity(), data);
		lv_financial_data.setAdapter(adapter);

		registerForContextMenu(lv_financial_data);

		// 初始
		btn_signedn_start = (Button) tabHost
				.findViewById(R.id.btn_sigdedn_start);

		btn_signedn_end = (Button) tabHost.findViewById(R.id.btn_sigdedn_end);
		btn_signedn_end.setOnClickListener(this);
		btn_signedn_start.setOnClickListener(this);
		et_signedn_start = (EditText) tabHost
				.findViewById(R.id.et_signedn_start);
		et_signedn_end = (EditText) tabHost.findViewById(R.id.et_signedn_end);

		btn_signed_query = (Button) tabHost.findViewById(R.id.btn_signed_query);
		btn_signed_query.setOnClickListener(this);

		// 签约类型 状态
		sp_signedn_type = (Spinner) tabHost.findViewById(R.id.sp_signedn_type);

		sp_signedn_status = (Spinner) tabHost
				.findViewById(R.id.sp_signedn_status);

		ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(
				getActivity(), R.layout.spinner_item, R.id.tv_idcard_item,
				getResources().getStringArray(R.array.sp_signedn_type));
		sp_signedn_type.setAdapter(typeAdapter);
		ArrayAdapter<String> statusAdapter = new ArrayAdapter<String>(
				getActivity(), R.layout.spinner_item, R.id.tv_idcard_item,
				getResources().getStringArray(R.array.sp_signedn_status));
		sp_signedn_status.setAdapter(statusAdapter);

		// 签约管理查询
		lv_signed_data = (ListView) tabHost.findViewById(R.id.lv_signed_data);
		View signedView = inflater.inflate(R.layout.financail_signed_list_item,
				null);

		// 设置标题 产品查询
		String[] lv_titles = getResources().getStringArray(
				R.array.lv_signed_title);
		TextView tv_signed_type = (TextView) signedView
				.findViewById(R.id.tv_signed_type);
		tv_signed_type.setText(lv_titles[0]);
		TextView tv_signed_customerID = (TextView) signedView
				.findViewById(R.id.tv_signed_customerID);
		tv_signed_customerID.setText(lv_titles[1]);
		TextView tv_signed_customerName = (TextView) signedView
				.findViewById(R.id.tv_signed_customerName);
		tv_signed_customerName.setText(lv_titles[2]);
		TextView tv_signed_startDate = (TextView) signedView
				.findViewById(R.id.tv_signed_startDate);
		tv_signed_startDate.setText(lv_titles[3]);
		TextView tv_signed_status = (TextView) signedView
				.findViewById(R.id.tv_signed_status);
		tv_signed_status.setText(lv_titles[4]);

		TextView tv_signed_remark = (TextView) signedView
				.findViewById(R.id.tv_signed_remark);
		tv_signed_remark.setText(lv_titles[5]);
		lv_signed_data.addHeaderView(signedView, null, false);
		List<Signed> signedData = new ArrayList<Signed>();
		for (int i = 0; i < 6; i++) {
			signedData.add(new Signed("233442422", "李淑", "理财产品开户", new Date(),
					"新建", "vip开户"));
		}
		SignedListViewAdapter signedListViewAdapter = new SignedListViewAdapter(
				getActivity(), signedData);

		lv_signed_data.setAdapter(signedListViewAdapter);

		lv_signed_data
				.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

					@Override
					public void onCreateContextMenu(ContextMenu menu, View v,
							ContextMenuInfo menuInfo) {

						menu.add(0, 3, 0, "详细信息");
						menu.add(0, 4, 0, "撤销");
					}

				});

		// 新建签约
		btn_signed_create = (Button) tabHost
				.findViewById(R.id.btn_signed_create);
		btn_signed_create.setOnClickListener(this);

		// 确认签约
		btn_signed_affirm = (Button) tabHost
				.findViewById(R.id.btn_signed_affirm);
		btn_signed_affirm.setOnClickListener(this);
		//确认  返回
		btn_signed_ok=(Button) tabHost.findViewById(R.id.btn_signed_ok);
		btn_signed_ok.setOnClickListener(this);
		btn_signed_back=(Button) tabHost.findViewById(R.id.btn_signed_back);
		btn_signed_back.setOnClickListener(this);
		
		return tabHost;
	}

	/***
	 * 点选按钮监听事件
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rbtn_title_fpm_search:
			tabHost.setCurrentTab(0);
			break;
		case R.id.rbtn_title_fpm_signedn:
			tabHost.setCurrentTab(1);
			break;
		case R.id.rbtn_title_fpm_sell:
			tabHost.setCurrentTab(2);
			break;
		case R.id.btn_signed_affirm:
			// 确认签约
			tabHost.setCurrentTab(7);
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_fip_query:
			// 查询
			tabHost.setCurrentTab(3);
			break;
		case R.id.btn_sigdedn_start:

			DatePickerDialog dialog = new DatePickerDialog(getActivity(),
					new DatePickerDialog.OnDateSetListener() {

						@Override
						public void onDateSet(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {

							StringBuilder builder = new StringBuilder();
							builder.append(year);
							builder.append("年");
							builder.append(monthOfYear);
							builder.append("月");
							builder.append(dayOfMonth);
							builder.append("日");
							Log.d("tag", "...日期:" + builder.toString());
							et_signedn_start.setText(builder.toString());
						}
					}, 2000, 02, 10);
			dialog.show();
			break;
		case R.id.btn_sigdedn_end:
			Calendar date = Calendar.getInstance();
			date.setTimeInMillis(System.currentTimeMillis());
			DatePickerDialog dialogEnd = new DatePickerDialog(getActivity(),
					new DatePickerDialog.OnDateSetListener() {

						@Override
						public void onDateSet(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {

							StringBuilder builder = new StringBuilder();
							builder.append(year);
							builder.append("年");
							builder.append(monthOfYear);
							builder.append("月");
							builder.append(dayOfMonth);
							builder.append("日");

							et_signedn_end.setText(builder.toString());
						}
					}, date.get(Calendar.YEAR), date.get(Calendar.MONTH),
					date.get(Calendar.DATE));
			dialogEnd.show();
			break;
		case R.id.btn_signed_query:
			tabHost.setCurrentTab(5);
			break;
		case R.id.btn_signed_create:
			// 新建签约
			tabHost.setCurrentTab(6);
			break;
		case R.id.btn_signed_back:
			tabHost.setCurrentTab(6);
			break;
		case R.id.btn_signed_ok:
			tabHost.setCurrentTab(8);
			break;
		default:
			break;
		}
	}

	/***
	 * 上下文菜单
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.addSubMenu(0, 0, 0, "详细信息");
		menu.addSubMenu(0, 1, 1, "产品销售");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Log.d("tag", item.getItemId() + "---" + item.getTitle() + "--");
		switch (item.getItemId()) {
		case 0:
			tabHost.setCurrentTab(4);
			break;

		case 1:

			break;
		case 3:

			break;

		case 4:
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle("提示");
			builder.setMessage("您将撤销当前签约产品？");
			builder.setNegativeButton("取消", new AlertDialog.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			
			builder.show();
			break;
		}

		return true;
	}

}